package gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;

import evolutionaryLoop.EvolutionaryParameters;
import evolutionaryProblems.Problem;

public class SettingPanel extends JPanel{

	protected static final long serialVersionUID = 1L;
	protected Problem problem; 
	protected EvolutionaryParameters parameters;
	protected JTextField nofChildren;
	protected JTextField nofAdults;
	protected JTextField nofGenerations;
	protected JTextField crossoverRate;
	protected JTextField mutationRate;
	protected JTextField elitism;
	protected JButton resetButton;
	protected JButton startButton;
	protected JToggleButton pauseButton;
	
	public SettingPanel(Problem problem, EvolutionaryParameters parameters){
		this.problem = problem; 
		this.parameters = parameters;
		this.setLayout(new GridBagLayout()); 
		initComponents(); 
	}
	
	protected void initComponents(){
		startButton = new JButton("Start");
		startButton.addActionListener(new StartSimulation());
		addComponent(startButton, 0, 0); 
		
		pauseButton = new JToggleButton("Pause");
		pauseButton.addActionListener(new PauseSimulation()); 
		addComponent(pauseButton, 1, 0); 
		
		resetButton = new JButton("Reset");
		resetButton.addActionListener(new RestartSimulation()); 
		addComponent(resetButton, 2, 0); 
		
		
		JLabel nofChildrenLabel = new JLabel("nof children");
		addComponent(nofChildrenLabel, 0, 1); 
		nofChildren = new JTextField();
		nofChildren.setColumns(10);
		addComponent(nofChildren, 1, 1);
		
		JLabel nofAdultsLabel = new JLabel("nof adults");
		addComponent(nofAdultsLabel, 0, 2); 
		nofAdults = new JTextField();
		nofAdults.setColumns(10);
		addComponent(nofAdults, 1, 2); 
		
		JLabel nofGenerationsLabel = new JLabel("nof generations");
		addComponent(nofGenerationsLabel, 0, 3); 
		nofGenerations = new JTextField();
		nofGenerations.setColumns(10);
		addComponent(nofGenerations, 1, 3);
		
		JLabel crossoverRateLabel = new JLabel("Crossover rate");
		addComponent(crossoverRateLabel, 2, 1);
		crossoverRate = new JTextField();
		crossoverRate.setColumns(10);
		addComponent(crossoverRate, 3, 1); 
		
		JLabel mutationRateLabel = new JLabel("Mutation rate");
		addComponent(mutationRateLabel, 2, 2);
		mutationRate = new JTextField();
		mutationRate.setColumns(10);
		addComponent(mutationRate, 3, 2); 
		
		JLabel elitismLabel = new JLabel("Elitism");
		addComponent(elitismLabel, 2, 3);
		elitism = new JTextField();
		elitism.setColumns(10);
		addComponent(elitism, 3, 3); 
		
	}
	
	protected void readParameters(){
		parameters.setElitims(Integer.parseInt(this.elitism.getText()));
		parameters.setMutationRate(Double.parseDouble(this.mutationRate.getText())); 
		parameters.setCrossoverRate(Double.parseDouble(this.crossoverRate.getText())); 
		parameters.setNumOfChildenToBeGenerated(Integer.parseInt(this.nofChildren.getText())); 
		parameters.setPopulationSize(Integer.parseInt(this.nofAdults.getText())); 
		parameters.setMaxNumberOfIterations(Integer.parseInt(this.nofGenerations.getText()));
	}
	
	public void showDefaultParameters(){
		this.elitism.setText(Integer.toString(parameters.getElitism())); 
		this.mutationRate.setText(Double.toString(parameters.getMutationRate())); 
		this.crossoverRate.setText(Double.toString(parameters.getCrossoverRate())); 
		this.nofChildren.setText(Integer.toString(parameters.getNumOfChildenToBeGenerated())); 
		this.nofAdults.setText(Integer.toString(parameters.getPopulationSize())); 
		this.nofGenerations.setText(Integer.toString(1000));
	}
	
	protected void addComponent(JComponent component, int x, int y){
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets(2, 2, 2, 2); 
		constraints.anchor = GridBagConstraints.WEST; 
		constraints.gridx = x; 
		constraints.gridy = y; 
		this.add(component, constraints); 
	}
	
	class StartSimulation implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			readParameters();
			problem.startSimulation();
			startButton.setEnabled(false);
		}
		
	}

	class RestartSimulation implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			problem.killSimulation();
			removeAll();
			initComponents();
			showDefaultParameters();
//			startButton.setEnabled(true);
			revalidate();
			repaint();
		}
	}
	
	class PauseSimulation implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			JToggleButton btn = (JToggleButton) arg0.getSource();
			if (btn.isSelected()) {
				btn.setText("Continue");
				problem.pauseSimulation();
			}
			else {
				problem.continueSimulation();
				btn.setText("Pause");
			}
		}
		
	}

	
}
